<?php
/**
 * 星悦媒体中心库
 */
namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\EditorModel;
use Common\Model\ArticleModel;
use Common\Model\UtilsModel;
use Think\Page;
class CenterController extends BaseController{
	const CHANNEL_TYPE_ARTICLE	=1;
	const CHANNEL_TYPE_CASE		=2;
	/**
	 * 文章列表页
	 * Author: shijy
	 */
	public function index(){
			if(IS_AJAX){
				$page		= I('post.page',1,'int');
				$pageSize	= I('post.pageSize',1,'int');
				$keyword    = I('post.keyword');

				$contentType= I('post.contentType');
            	//$system     = I('post.sys');

				$param = array(
					//'sys'		=> $system,
					'keyword'  => $keyword,
					'type'		 => self::CHANNEL_TYPE_ARTICLE,
					'contentType'=>$contentType,

					'page'		=> $page,
					'pageSize'	=> $pageSize,

				);
				$return = UtilsModel::broadcast('Article_getList',$param,'starCenter');
				if($return['status']!=0){
					$this->ajaxReturn($return);
				}else{
					$this->smarty->assign('list',$return['data']);
					$html=$this->smarty->fetch('Center/list.html');
					unset($return['data']);
					$return['dataHtml']=$html;
					$this->ajaxReturn($return);
				}
			}
			else {
				$return = UtilsModel::broadcast('Article_getContentType',null,'starCenter');
				$contentType=$return['data'];
				$this->smarty->assign('contentType',$contentType);

				$this->smarty->assign('CENTER_URL',U('index'));
				$this->smarty->display('Center/index.html');
			}
	}

	/**
	 * 拉取文章
	 * Author: shijy
	 */
	public function pull(){
		$id=I('get.id',0,'int');
		if($id==0){
			$this->ajaxReturn(
				array('status'=>1,'msg'=>'请指定文章')
			);
		}

		$return = UtilsModel::broadcast('Article_pull',array('id'=>$id,'sys'=>CURRENT_COMPANY),'starCenter');
		if($return['status']!=0){
			$this->ajaxReturn($return);
		}

		$param=$return['data'];

		$doResult       = UtilsModel::NativeThumb($param['content']);
		$relativePhoto  = $doResult[0];
		$content        = $doResult[1];

		if (array_key_exists('summary',$param)){
			$summary        = $param['summary'];
		}else{
			$summary        = mb_substr(strip_tags($content),0,50);
		}

		$listPic = UtilsModel::saveImageByURL($param['listPic']);


		$editorID = session(C('USER_AUTH_KEY'))+0;

		$newData = array(
			'editor'=>$editorID,
			'title'=>$param['title'],
			'listPic'=>$listPic,
			'summary'=>$summary,
			'content'=>$content,
			'uid'=>0,
			'nickname'=>'',
			'relativePhoto'=>implode(',',$relativePhoto),
			'belong_store'=>session('belong_store'),
			'addTime'=>time()
		);

		$result=ArticleModel::getInstance()->add($newData);
		if($result === false){
			$this->ajaxReturn(array('msg'=>'文章拉取失败','status'=>1));
		}
		$this->ajaxReturn(array('msg'=>'中心库接收成功','status'=>0,'id'=>$result));

	}


	/**
	 * 案例列表
	 * Author: shijy
	 */
	public function cases(){
		if(IS_AJAX){
			$page		= I('post.page',1,'int');
			$pageSize	= I('post.pageSize',1,'int');
			$keyword    = I('post.keyword');
			$tp     	= I('post.tp');
			//$system   = I('post.sys');

			$param = array(
				//'sys'		 => $system,
				'keyword'   => $keyword,
				'type'		 => self::CHANNEL_TYPE_CASE,
				'tp'		 => $tp,

				'page'		=> $page,
				'pageSize'	=> $pageSize,
			);
			$return = UtilsModel::broadcast('Article_getList',$param,'starCenter');
			if($return['status']!=0){
				$this->ajaxReturn($return);
			}else{

				foreach ($return['data'] as $k=>$v){
					if(strpos($v['quoteSystem'],CURRENT_COMPANY)===false){
						$return['data'][$k]['quoteSystem']=0;
					}else{
						$return['data'][$k]['quoteSystem']=1;
						$return['data'][$k]['sn']=md5($v['id'].'_'.CURRENT_COMPANY);
					}
				}
				$this->smarty->assign('list',$return['data']);
				$html=$this->smarty->fetch('Center/caseList.html');
				unset($return['data']);
				$return['dataHtml']=$html;
				$this->ajaxReturn($return);
			}
		}
		else {

			$tp=I('get.tp',1,'int');
			$this->smarty->assign('tp',$tp);
			$this->smarty->assign('QUERY_URL',U('cases'));
			$this->smarty->assign('MEDIA_CENTER_URL',MEDIA_CENTER_URL);
			$this->smarty->display('Center/caseIndex.html');
		}
	}

	/**
	 * 使用案例
	 * Author: shijy
	 */
	public function useCase(){
		$id=I('get.id',0,'int');

		if($id==0){
			$this->ajaxReturn(
				array('status'=>1,'msg'=>'请指定案例')
			);
		}

		$return = UtilsModel::broadcast('Article_useCase',array('id'=>$id,'sys'=>CURRENT_COMPANY),'starCenter');
		if($return['status']!=0){
			$this->ajaxReturn($return);
		}
		$return['sn']=md5($id.'_'.CURRENT_COMPANY);

		$this->ajaxReturn($return);

	}
}
